using System;
using System.Collections;
using System.Configuration.Install;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;
using Microsoft.Win32;

namespace ItemTemplateInstaller
{
	public abstract class VSGeneratorRegistrationBase : InstallerBase
	{
		protected abstract string ProductTitle { get; }
		protected abstract string ProductSetupCode { get; }

		protected void InstallInternal(IDictionary stateSaver)
		{
			if (string.IsNullOrEmpty(InstallDir))
				return;

			using (var form = new Progress())
			{
				form.Show();
				form.Focus();
				SetupVS(form);
				form.Close();
			}
			stateSaver.Add(ProductSetupCode, true);
		}

		protected void UninstallInternal(IDictionary stateSaver)
		{
			if (stateSaver == null)
				return;

			if (!stateSaver.Contains(ProductSetupCode))
				return;

			if (string.IsNullOrEmpty(InstallDir))
				return;

			using (var form = new Progress())
			{
				form.Show();
				form.Focus();
				SetupVS(form);
				form.Close();
			}
		}

		private void SetupVS(Progress form)
		{
			form.Message = "Running setup on " + ProductTitle;
			Application.DoEvents();

			var process = Process.Start(DevEnvLocation, "/setup");

			if (process != null)
				process.WaitForExit();

			Application.DoEvents();
			form.Message = "Setup complete.";
		}

	}
}